from DBA.dbmanager import DBInterface

class Campagne:
	
	def __init__(self):
		self.id			= {"key":"ID","value":""}
		self.name		= {"key":"nom","value":""}
		self.env		= {"key":"env","value":""}
		self.radiostat		= {"key":"statradio","value":""}
		self.traficstat		= {"key":"stattrafic","value":""}
		self.manuel		= {"key":"manuel","value":""}
		self.automatique	= {"key":"automatique","value":""}
		self.step		= {"key":"step","value":""}
		self.ensMesures		= []
		self.__interfacebase	= DBInterface()
		self.id["value"]	= str(self.__interfacebase.createNewID());
	
	def addMesure(self,mesure):
		self.ensMesures.append(mesure)
	
	def nbMesures(self):
		return self.ensMesures.count()

	def createByDico(self,dico):
		self.name["value"] 		= dico[self.name["key"]]
		self.env["value"] 		= dico[self.env["key"]]
		self.radiostat["value"] 	= dico[self.radiostat["key"]]
		self.traficstat["value"] 	= dico[self.traficstat["key"]]
		self.manuel["value"] 		= dico[self.manuel["key"]]
		self.automatique["value"] 	= dico[self.automatique["key"]]
		self.step["value"] 		= dico[self.step["key"]]

	def insertToDb(self,idSalle,idAP):
		dico = self.createCampagneDico(idAP,idSalle)
		self.__interfacebase.insert(self.__interfacebase.campagnetablename,self.__interfacebase.campagnemapping,dico)
		inter = self.createIntermediaireDico(idAP,idSalle)
		self.__interfacebase.insert(self.__interfacebase.ref_AP_Salle_Campagnetablename,self.__interfacebase.ref_AP_Salle_CampagneMapping,inter)
		for mesure in self.ensMesures:
			mesure.insertToDb(self.id["value"])

	def updateIntoDb(self,idSalle,idAP):
		dico = self.createCampagneDico(idAP,idSalle)
		self.__interfacebase.update(self.__interfacebase.campagnetablename,self.__interfacebase.campagnemapping,dico)
		inter = self.createIntermediaireDico(idAP,idSalle)
		self.__interfacebase.update(self.__interfacebase.ref_AP_Salle_Campagnetablename,self.__interfacebase.ref_AP_Salle_CampagneMapping,inter)
		for mesure in self.ensMesures:
			mesure.updateIntoDb(self.id["value"])
	
	def createCampagneDico(self,idAP,idSalle):
		dico ={}
		dico[self.id["key"]]		= self.id["value"]
		dico["link_sa"]			= idSalle
		dico["link_ap"]			= idAP
		dico[self.name["key"]]		= self.name["value"]
		dico[self.env["key"]]		= self.env["value"]
		dico[self.radiostat["key"]]	= self.radiostat["value"]
		dico[self.traficstat["key"]]	= self.traficstat["value"]
		dico[self.manuel["key"]]	= self.manuel["value"]
		dico[self.automatique["key"]]	= self.automatique["value"]
		dico[self.step["key"]]		= self.step["value"]
		return dico

	def createIntermediaireDico(self,idAP,idSalle):
		dico ={}
		dico[self.id["key"]]		= self.id["value"]
		dico["link_sa"]			= idSalle
		dico["link_ap"]			= idAP
		return dico

if __name__=="__main__":
	bat = Salle()	
	bat.name["value"]		= "test";
	#bat.description["value"]	= "t";
	bat.xposition["value"]		= "tz";
	bat.yposition["value"]		= "ta";
	bat.zposition["value"]		= "te";
	bat.height["value"]		= "tq";
	bat.width["value"]		= "t";
	
	print bat
